<template>
    <view>
        <nova-pref v-if="auth" :title="title" :placeholder="placeholder" :value="loc.name" :arrow="true" @click.native="_chooseLocation()"></nova-pref>
        <nova-pref v-else :title="title" :arrow="true">
            <button open-type="openSetting" id="chooseLocation" @opensetting="_chooseLocation()" v-show="false"></button>
            <label for="chooseLocation" v-if="loc.name">{{ loc.name }}</label>
            <label for="chooseLocation" v-else class="placeholder">{{ placeholder }}</label>
        </nova-pref>
    </view>
</template>

<script>
import NovaPref from '../../components/nova/pref.vue';
export default {
    name: 'choose-location',
    components: {
        NovaPref
    },
    props: {
        value: {
            type: String,
            default: ''
        },
        title: {
            type: String,
            default: ''
        },
        placeholder: {
            //没有值时显示的占位
            type: String,
            default: ''
        },
        loc: {
            type: Object,
            default: () => {
                return {
                    latitude: '',
                    longitude: '',
                    name: '',
                    address: ''
                };
            }
        }
    },
    data() {
        return {
            auth: false
        };
    },
    created() {
        let that = this;
        // #ifndef APP-PLUS || H5
        uni.getSetting({
            success: res => {
                that.auth = res.authSetting['scope.userLocation'];
                console.log('auth ? ' + that.auth);
            }
        });

        // #endif
    },
    methods: {
        _openSetting() {
            let that = this;
            uni.openSetting({
                success(res) {
                    console.log('open setting', res.authSetting);
                    that._checkAuth(res.authSetting);
                },
                fail(err) {
                    console.error('open settings err', err);
                    that.$emit('error', err);
                }
            });
        },
        _checkAuth(auth, needOpen) {
            let that = this;
            console.log(auth['scope.userLocation']);
            if (auth['scope.userLocation']) {
                that.auth = true;
                that._chooseLocation();
            }
        },
        _getLocation() {
            console.log('getLocation');
            this.auth = true;
            let that = this;
            uni.getLocation({
                success(res) {
                    console.log('getLocation', res);
                    Object.assign(that.pos, res);
                },
                fail(err) {
                    console.error('getLocation err', err);
                    that.$emit('error', err);
                }
            });
        },
        _chooseLocation() {
            let that = this;
            that.auth = true;
            // #ifdef MP-TOUTIAO || MP-QQ
            that.$emit('error', {});
            return;
            // #endif
            // #ifndef MP-TOUTIAO || MP-QQ
            uni.chooseLocation({
                success(res) {
                    console.log('choose ', res);
                    //that.loc = res;
                    that.$emit('change', res);
                },
                fail(err) {
                    console.error('chooseLocation err', err);
                    that.$emit('error', err);
                },
                // #ifdef MP-WEIXIN
                latitude: that.loc.latitude,
                longitude: that.loc.longitude
                // #endif
            });
            // #endif
        }
    }
};
</script>

<style></style>
